import java.util.List;

public class MySingleList {
    static class ListNode {   //节点内部类
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;    //不初始化了，让它默认成null

    public void createList() {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
    }

    //头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);   //先申请一个值为data的结点
        node.next = head;     //技巧：链表插入 先和后面的建立联系再更新head
        head = node;
    }

    //尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(cur == null) {   //如果链表为空，直接把node给给head
            this.head = node;
        }
        while(cur.next != null) {   //遍历走到尾结点
            cur = cur.next;
        }
        cur.next = node;
    }


    //任意位置插入  1.让cur走index-1步  2.改变指向
    public void addIndex(int index,int data) {
        if(index < 0 || index > this.size()) {
            System.out.println("index位置不合法！");
            throw new IndexWrongFulException("index位置不合法！");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == this.size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        //1.先走index-1步,cur走到要插入位置的前一个
        ListNode cur = findIndexSubOne(index);
        //2.修改指向
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = this.head;
        while (index-1 != 0) {
            cur=cur.next;
            index--;
        }
        return cur;
    }

    public int size() {
        int cnt = 0;
        ListNode cur = this.head;
        while(cur != null) {
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

    //删除第一个key的结点
    public void Remove(int key) {
        if(this.head == null) {   //空链表
            return;
        }
        if(this.head.val == key) {   //首结点就是要删除的
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPrevOfKey(key);  //找到key的前驱
        if(cur == null) {
            System.out.println("没有找到该数字");
            return;
        }
        ListNode del = cur.next;  //要删的结点
        //这句才是真正删除
        cur.next = del.next;
    }

    //找到key的前一个结点
    private ListNode findPrevOfKey(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }

    public void clear() {
        this.head = null;
    }

}
